package com.domain.query;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;

/**
 * 分页查询实体
 * @author 17645
 * @date 2024/06/17
 */
@Data
public class PageQuery {
    /**
     * 当前页码
     */
    private Integer pageNo =1;
    /**
     * 分页大小
     */
    private Integer limit =5;
    /**
     * 排序字段
     */
    private String sortBy;
    /**
     * 是否升序
     */
    private Boolean inAsc =true;

    public <T>Page <T> toMpPage(OrderItem...items){
        //1.分页条件
        Page<T> page = new Page<>(pageNo,limit);
        OrderItem orderItem = new OrderItem();
        //2.排序条件
        if (sortBy!=null){
            //不为空
            orderItem.setColumn(sortBy);
            orderItem.setAsc(inAsc);
            page.addOrder(orderItem);
        }else if(items!=null) {
            //为空 默认排序
            page.addOrder(items);
        }
        return page;
    }

    public <T> Page<T> toMpPageDefaultSortByCreateTime(){
        return toMpPage(OrderItem.desc("create_time"));
    }

    public <T> Page<T> toMpPageDefaultSortByUpdateTime(){
        return toMpPage(OrderItem.desc("update_time"));
    }

    public <T> Page<T> toMpPage(String defaultSortBy,Boolean defaultAsc){
        OrderItem orderItem = new OrderItem();
        orderItem.setColumn(defaultSortBy);
        orderItem.setAsc(defaultAsc);
        return toMpPage(orderItem);
    }
}
